/*
 * gyroDriver.c
 *
 *  Created on: Feb 14, 2009
 *      Author: campbelm
 */

#include <avr/io.h>
#include <stdint.h>

void gyro_init() {
	// Set all of portF to input
	DDRF = 0x00;

	// Disable digital buffers
	DIDR0 = 0xFF;

	ADCSRA |= (1 << ADEN); // Enable ADC
	ADCSRA |= (1 << ADPS1) | (1 << ADPS2); // 64 time divisor
}

int8_t getYRateSample() {
	// The actual sample
	uint16_t sample;
	ADMUX = (1 << REFS0);
	ADMUX |= (1 << MUX2); // Select the port (PORTF4) for conversion
	ADMUX |= (1 << ADLAR); // Put the more important bits in the the high return

	int i;
	for (i = 0; i < 2; i++) {
		ADCSRA |= (1 << ADSC); // Start the conversion
		while (ADCSRA & (1 << ADSC))
			; // Loop until the conversion ends
		sample = ADCH;
	}

	return sample - 127;
}

int8_t getZAccelerationSample() {
	// The actual sample
	uint16_t sample;
	ADMUX = (1 << REFS0);
	ADMUX |= (1 << MUX2) | (1 << MUX0); // Select the port (PORTF4) for conversion
	ADMUX |= (1 << ADLAR);
	int i;
	for (i = 0; i < 2; i++) {
		ADCSRA |= (1 << ADSC); // Start the conversion
		while (ADCSRA & (1 << ADSC))
			; // Loop until the conversion ends
		sample = ADCH;
	}

	return -(sample + 67);
}
